Skip to content

http: optimize corked writes and header pair tracking - #64887

Closed
GetThatCookie wants to merge 2 commits into
nodejs:mainfrom
GetThatCookie:main
Closed

http: optimize corked writes and header pair tracking#64887
GetThatCookie wants to merge 2 commits into
nodejs:mainfrom
GetThatCookie:main

Conversation

@GetThatCookie

Copy link
Copy Markdown

This PR contains two focused HTTP performance improvements without adding or
changing public APIs.

Optimize corked outgoing writes

OutgoingMessage.uncork() previously uncorked the underlying socket before
flushing its buffered HTTP chunk. The individual framing writes therefore
bypassed socket-level batching.

Buffered body data could also be flushed after the terminating chunk when
end() was called while the message was still corked.

This change:

  • flushes buffered HTTP chunks before uncorking the socket;
  • flushes buffered body data before writing the terminating chunk;
  • preserves callback ordering, backpressure, trailers, TLS, and pipelining;
  • prevents a synchronous drain listener from writing after the terminating
    chunk.

Cache maxHeaderPairs

The HTTP parser previously read the JavaScript maxHeaderPairs property for
every header field.

This change caches the value once per independent header section and
invalidates it for:

  • trailers;
  • the next pipelined message;
  • parser reinitialization.

The parser benchmark now explicitly uses the production default of 2000 header
pairs because direct benchmark parsers bypass cleanParser().

Performance

Local macOS arm64 Release results, using the median of three short runs:

Scenario Before After Improvement
Core HTTP corked writes 17.3k req/s 59.5k req/s 3.44x
Express corked writes 16.6k req/s 50.8k req/s 3.06x

Parser benchmark improvements:

Header fields Improvement
5 22%
17 54%

End-to-end HTTP header benchmarks improved by approximately 4.8% to 7.1%.

Tests

  • Release build completed successfully.
  • ESLint, cpplint, checkimports, and git diff --check passed.
  • The focused HTTP test set passed across 80 repeated executions.
  • All existing test-http-parser-* tests passed.
  • All existing test-http-outgoing-* tests passed.
  • Additional HTTP chunking, trailer, and pipelining tests passed.
  • The new tests cover HTTP and HTTPS, client and server messages, nested
    corking, callback ordering, trailers, backpressure, and queued pipelined
    responses.

Documentation was not changed because no public API or documented behavior is
introduced.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http Issues or PRs related to the http subsystem. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. needs-ci PRs that need a full CI run. labels Jul 31, 2026
@GetThatCookie

Copy link
Copy Markdown
Author

Superseded by the focused changes in #64987 and #64988. Closing this in favor of those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants